package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.GoodSearchService;
import org.darkforest.common.services.GoodsCommonService;
import org.darkforest.common.services.GoodsService;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.GoodsCommonSearchBean;
import org.darkforest.model.GoodsDetail;
import org.darkforest.model.RestEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品控制类
 *
 * @auth bingo
 * @create 2017-10-10-18:55
 */
@RestController
@RequestMapping(value = "goods")
@Api(value = "goods", description = "商品",produces= MediaType.APPLICATION_JSON_VALUE)
public class GoodsController {

    @Autowired
    private GoodsCommonService goodsCommonService;
    @Autowired
    private GoodSearchService goodsService;
    /**
     * 商品详情
     * @param goodsCommonid
     * @return
     */
    @RequestMapping(value = "info",method = RequestMethod.GET)
    @ApiOperation(value = "根据商品ID获取商品详情", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity info(Integer goodsCommonid){
        try {
            GoodsDetail goodsCommon = goodsCommonService.getGoodsDetail(goodsCommonid);
            return RestEntity.success(goodsCommon);

        }catch (Exception e){
            return RestEntity.failed();

        }
    }

    /**
     * 根据商品分类ID 查询列表
     * @return
     */
    @RequestMapping(value = "byClassId",method = RequestMethod.GET)
    @ApiOperation(value = "根据商品分类ID查询列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity byClassId(Integer classId,@RequestParam(defaultValue = "0") int start,@RequestParam(defaultValue = "10") int size){
        GoodsCommon param = new GoodsCommon();
        param.setPageSwitch(true);
        param.setStart(start);
        param.setPageSize(size);
        param.setGcId1(classId);
        List<GoodsCommon> goodsCommonList = goodsCommonService.findSimpleGoodsCommonByCond(param);
        return RestEntity.success(goodsCommonList);
    }


    /**
     * 根据商品关键词搜索
     * @param key
     * @return
     */
    @RequestMapping(value = "search",method = RequestMethod.POST)
    @ApiOperation(value = "根据商品关键词搜索", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity searchByKey(@RequestParam(required = false, defaultValue = "") String key,
                                    @RequestParam(required = false, defaultValue = "1") int p,
                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                    @RequestParam(required = false, defaultValue = "") String orderByType,
                                    @RequestParam(required = false, defaultValue = "goods_addtime") String orderByColum,
                                    Integer goodsTagId, String recommendType,
                                    Integer goodsClassId, Integer brandId, BigDecimal startPrice, BigDecimal endPrice){

        GoodsCommonSearchBean goodsCommonSearchBean = new GoodsCommonSearchBean();

        goodsCommonSearchBean.setGoodsName(key == null ? "" : key.replaceAll("[\\x{10000}-\\x{10FFFF}]", ""));
        goodsCommonSearchBean.setGoodsClassId(goodsClassId);
        goodsCommonSearchBean.setBrandId(brandId);
        goodsCommonSearchBean.setStartPrice(startPrice);
        goodsCommonSearchBean.setEndPrice(endPrice);
        goodsCommonSearchBean.setStart(p);
        goodsCommonSearchBean.setPageSize(pageSize);
        goodsCommonSearchBean.setOrderByType(orderByType);
        goodsCommonSearchBean.setOrderByColum(orderByColum);

        // 检索的关联table
        String tableName = "bw_goods_common t, bw_store s";
        // 商品标签 （按对象送时的条件）
        if (goodsTagId != null) {
            tableName = tableName + ", bw_goodscommon_tag_relation gtr";
            goodsCommonSearchBean.setGoodsTagId(goodsTagId);
        }
        // 活动专区用条件
        if (recommendType != null && !"".equals(recommendType)) {
            tableName = tableName + ", bw_active_area_goods aag";
            goodsCommonSearchBean.setRecommendType(recommendType);
        }
        goodsCommonSearchBean.setTableName(tableName);

        List<GoodsCommon> goodsCommonList= goodsService.listGoodsCommonByCondition(goodsCommonSearchBean);
        return RestEntity.success(goodsCommonList);
    }

    /**
     * 根据商品关键词搜索
     * @param key
     * @return
     */
    @RequestMapping(value = "searchSupportGoods",method = RequestMethod.POST)
    @ApiOperation(value = "爱心或扶贫县商品", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity searchSupportGoods(@RequestParam(required = false, defaultValue = "") String key,
                                  @RequestParam(required = false, defaultValue = "1") int p,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false, defaultValue = "") String orderByType,
                                  @RequestParam(required = false, defaultValue = "goods_addtime") String orderByColum,
                                  @RequestParam(required = false, defaultValue = "1") int scId,
                                  @RequestParam(required = false, defaultValue = "0") int storeId,
                                  @RequestParam(required = false, defaultValue = "0") Integer countyId,
                                  @RequestParam(required = false, defaultValue = "0") Integer companyId,
                                  String recommendType, Integer goodsTagId,
                                  Integer goodsClassId, Integer brandId, BigDecimal startPrice, BigDecimal endPrice){

        GoodsCommonSearchBean goodsCommonSearchBean = new GoodsCommonSearchBean();

        goodsCommonSearchBean.setGoodsName(key == null ? "" : key.replaceAll("[\\x{10000}-\\x{10FFFF}]", ""));
        goodsCommonSearchBean.setGoodsClassId(goodsClassId);
        goodsCommonSearchBean.setBrandId(brandId);
        goodsCommonSearchBean.setScId(scId);  // 店铺表的店铺分类  贫困县：scId = 1
        goodsCommonSearchBean.setStoreId(storeId);      // 商品表的storeId
        goodsCommonSearchBean.setCountyId(countyId);     //所属贫困县id
        goodsCommonSearchBean.setCompanyId(companyId);   //所属企业id

        goodsCommonSearchBean.setStartPrice(startPrice);
        goodsCommonSearchBean.setEndPrice(endPrice);
        goodsCommonSearchBean.setStart(p);
        goodsCommonSearchBean.setPageSize(pageSize);
        goodsCommonSearchBean.setOrderByType(orderByType);
        goodsCommonSearchBean.setOrderByColum(orderByColum);

        // 检索的关联table
        String tableName = "bw_goods_common t, bw_store s";
        // 商品标签
        if (goodsTagId != null) {
            tableName = tableName + ", bw_goodscommon_tag_relation gtr";
            goodsCommonSearchBean.setGoodsTagId(goodsTagId);
        }
        // 店铺关联条件
        if ((countyId != null && countyId.intValue() != 0) || (companyId != null && companyId.intValue() != 0)) {
            //tableName = tableName + ", bw_store s";
            if (countyId != null && countyId.intValue() != 0) {
                goodsCommonSearchBean.setCountyId(countyId);
            } else {
                goodsCommonSearchBean.setCompanyId(companyId);
            }
        }
        // 活动专区用条件
        if (recommendType != null && !"".equals(recommendType)) {
            tableName = tableName + ", bw_active_area_goods aag";
            goodsCommonSearchBean.setRecommendType(recommendType);
        }
        goodsCommonSearchBean.setTableName(tableName);

        List<GoodsCommon> goodsCommonList= goodsService.listGoodsCommonByCondition(goodsCommonSearchBean);
        return RestEntity.success(goodsCommonList);
    }
}
